/*
 * Package il.ac.biu.cs.grossmm.api_tests.keys
 * File PatternPatternMatchTest.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.junit_api_tests.keys;

import static il.ac.biu.cs.grossmm.api.keys.KeyPattern.*;
import static il.ac.biu.cs.grossmm.api.keys.PatternFactory.*;
import static il.ac.biu.cs.grossmm.api.presence.BaseVocabulary.*;
import static il.ac.biu.cs.grossmm.impl.keys.PatternMatcher.*;
import static org.junit.Assert.*;
import il.ac.biu.cs.grossmm.api.keys.KeyPattern;

import org.junit.Test;

/**
 * Contains series of tests for correctnes of pattern-pattern matching
 * algorithm and implementation
 */
public class PatternPatternMatchTest {
	@Test
    public void emptyToEmpty() {
        int c = compare(pattern(), pattern());
		
        assertEquals(EQUALITY, c);
    }
	
	
	@Test
    public void emptyToAnyKey() {
        int c = compare(pattern(), ANY_KEY);
		
        assertEquals(LEFT_RESTRICTION, c);
    }
	
	
	@Test
    public void emptyToAnySimpleKey() {
        int c = compare(pattern(), ANY_SIMPLE_KEY);
		
        assertEquals(LEFT_RESTRICTION, c);
    }
	
	@Test
    public void AnykeyToAnykey() {
        int c = compare(ANY_KEY, ANY_KEY);
		
        assertEquals(EQUALITY, c);
    }
	
	@Test
    public void AnykeyToAnySimplekey() {
        int c = compare(ANY_KEY, ANY_SIMPLE_KEY);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void StringkeyToAnySimplekey() {
        int c = compare(pattern(String.class), ANY_SIMPLE_KEY);
		
        assertEquals(LEFT_RESTRICTION, c);
    }
	
	@Test
    public void disjointValueClasses() {
		KeyPattern left = pattern(String.class);
		KeyPattern right = pattern(Integer.class);
        int c = compare(left, right);
		
        assertEquals(INTERSECTION, c);
    }
	
	@Test
    public void simpleMaskToExact() {
		KeyPattern left = maskPattern();
		KeyPattern right = pattern();
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void simpleMaskToExact2() {
		KeyPattern left = maskPattern(String.class);
		KeyPattern right = pattern(String.class);
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void simpleMaskToExact3() {
		KeyPattern left = maskPattern(Object.class);
		KeyPattern right = pattern(String.class);
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void simpleMaskToExact4() {
		KeyPattern left = maskPattern(String.class);
		KeyPattern right = pattern(Object.class);
        int c = compare(left, right);
		
        assertEquals(INTERSECTION, c);
    }
	
	@Test
    public void simpleMasks() {
		KeyPattern left = maskPattern();
		KeyPattern right = maskPattern();
        int c = compare(left, right);
		
        assertEquals(EQUALITY, c);
    }
	
	@Test
    public void simpleMasks2() {
		KeyPattern left = maskPattern(String.class);
		KeyPattern right = maskPattern(Object.class);
        int c = compare(left, right);
		
        assertEquals(LEFT_RESTRICTION, c);
    }
	
	@Test
    public void simpleMasks3() {
		KeyPattern left = maskPattern(String.class);
		KeyPattern right = maskPattern(Integer.class);
        int c = compare(left, right);
		
        assertEquals(INTERSECTION, c);
    }
	
	@Test
    public void simpleMasks4() {
		KeyPattern left = maskPattern(String.class);
		KeyPattern right = maskPattern("foo");
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void simpleMasks5() {
		KeyPattern left = maskPattern(String.class);
		KeyPattern right = maskPattern(123);
        int c = compare(left, right);
		
        assertEquals(DISJOINT, c);
    }
	
	@Test
    public void simpleMasks6() {
		KeyPattern left = maskPattern("foo");
		KeyPattern right = maskPattern("foo");
        int c = compare(left, right);
		
        assertEquals(EQUALITY, c);
    }
	
	@Test
    public void simpleMasks7() {
		KeyPattern left = maskPattern("foo");
		KeyPattern right = maskPattern("bar");
        int c = compare(left, right);
		
        assertEquals(DISJOINT, c);
    }
	
	@Test
    public void simpleMasks8() {
		KeyPattern left = maskPattern("foo");
		KeyPattern right = maskPattern(123);
        int c = compare(left, right);
		
        assertEquals(DISJOINT, c);
    }
	
	@Test
    public void complexPatterns1() {
		KeyPattern left = ANY_KEY;
		KeyPattern right = pattern(123, mandatory(CONTENT_TYPE));
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void complexPatterns2() {
		KeyPattern left = ANY_KEY;
		KeyPattern right = maskPattern(String.class, mandatory(CONTENT_TYPE));
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void complexPatterns3() {
		KeyPattern left = ANY_KEY;
		KeyPattern right = pattern("alice", optional(CONTENT_TYPE));
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void complexPatterns4() {
		KeyPattern left = ANY_KEY;
		KeyPattern right = maskPattern(123, optional(CONTENT_TYPE));
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void complexPatterns5() {
		KeyPattern left = ANY_SIMPLE_KEY;
		KeyPattern right = pattern(123, mandatory(CONTENT_TYPE));
        int c = compare(left, right);
		
        assertEquals(DISJOINT, c);
    }
	
	@Test
    public void complexPatterns6() {
		KeyPattern left = ANY_SIMPLE_KEY;
		KeyPattern right = maskPattern(String.class, mandatory(CONTENT_TYPE));
        int c = compare(left, right);
		
        assertEquals(DISJOINT, c);
    }
	
	@Test
    public void complexPatterns7() {
		KeyPattern left = ANY_SIMPLE_KEY;
		KeyPattern right = pattern("alice", optional(CONTENT_TYPE));
        int c = compare(left, right);
		
        assertEquals(INTERSECTION, c);
    }
	
	@Test
    public void complexPatterns8() {
		KeyPattern left = ANY_SIMPLE_KEY;
		KeyPattern right = maskPattern(123, optional(CONTENT_TYPE));
        int c = compare(left, right);
		
        assertEquals(INTERSECTION, c);
    }
	
	@Test
    public void complexPatterns9() {
		KeyPattern left = ANY_KEY;
		KeyPattern right = maskPattern(123, optional(CONTENT_TYPE));
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void complexPatterns10() {
		KeyPattern left = ANY_KEY;
		KeyPattern right = pattern("alice", optional(CONTENT_TYPE));
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void complexPatterns11() {
		KeyPattern left =  pattern(String.class,
			mandatory(CONTENT_TYPE, "Xml"),
			mandatory(RESOURCE_TYPE, "Presence"));
		KeyPattern right = pattern(String.class,
			mandatory(CONTENT_TYPE, "Xml"),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(LEFT_RESTRICTION, c);
    }
	
	@Test
    public void complexPatterns12() {
		KeyPattern left =  pattern(String.class,
			mandatory(CONTENT_TYPE, String.class),
			mandatory(RESOURCE_TYPE, "Presence"));
		KeyPattern right = pattern(String.class,
			mandatory(CONTENT_TYPE, "Xml"),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(INTERSECTION, c);
    }
	
	@Test
    public void complexPatterns13() {
		KeyPattern left =  pattern(String.class,
			mandatory(RESOURCE_TYPE, "Presence"));
		KeyPattern right = pattern(String.class,
			mandatory(CONTENT_TYPE, "Xml"),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(DISJOINT, c);
    }
	
	@Test
    public void complexPatterns14() {
		KeyPattern left =  pattern(String.class,
			mandatory(RESOURCE_TYPE, "Xml"));
			optional(FILTER, "no-sphere");
		KeyPattern right = pattern(String.class,
			mandatory(CONTENT_TYPE, "Xml"),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(DISJOINT, c);
    }
	
	@Test
    public void complexPatterns15() {
		KeyPattern left =  pattern(String.class,
			optional(CONTENT_TYPE, "Xml"),
			optional(RESOURCE_TYPE, "Presence"));
		KeyPattern right = pattern(String.class,
			optional(CONTENT_TYPE, "Xml"),
			optional(RESOURCE_TYPE, "Winfo"));
        int c = compare(left, right);
		
        assertEquals(INTERSECTION, c);
    }
	
	@Test
    public void complexPatterns16() {
		KeyPattern left = pattern(String.class,
			mandatory(CONTENT_TYPE, "Xml"),
			optional(RESOURCE_TYPE, "Presence"));
		KeyPattern right = pattern(String.class,
			optional(CONTENT_TYPE, "Pidf"),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(DISJOINT, c);
    }
	
	@Test
    public void complexPatterns17() {
		KeyPattern left = pattern(String.class,
			mandatory(CONTENT_TYPE, "Xml"),
			optional(RESOURCE_TYPE, String.class));
		KeyPattern right = pattern(String.class,
			optional(CONTENT_TYPE, String.class),
			mandatory(RESOURCE_TYPE, "Presence"));
        int c = compare(left, right);
		
        assertEquals(INTERSECTION, c);
    }
	
	@Test
    public void complexPatterns18() {
		KeyPattern left = maskPattern(String.class,
			mandatory(CONTENT_TYPE, "Xml"),
			optional(RESOURCE_TYPE, "Presence"));
		KeyPattern right = pattern(String.class,
			optional(CONTENT_TYPE, "Pidf"),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(DISJOINT, c);
    }
	
	@Test
    public void complexPatterns19() {
		KeyPattern left = maskPattern(String.class,
			mandatory(CONTENT_TYPE, String.class));
		KeyPattern right = pattern(String.class,
			optional(CONTENT_TYPE, "Pidf"),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(INTERSECTION, c);
    }
	
	@Test
    public void complexPatterns20() {
		KeyPattern left = maskPattern(String.class,
			optional(CONTENT_TYPE, String.class));
		KeyPattern right = pattern(String.class,
			optional(CONTENT_TYPE, "Pidf"),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
	
	@Test
    public void complexPatterns21() {
		KeyPattern left = maskPattern(String.class,
			optional(CONTENT_TYPE, String.class));
		KeyPattern right = pattern(String.class,
			optional(CONTENT_TYPE, ANY_SIMPLE_KEY),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(INTERSECTION, c);
    }
	
	@Test
    public void complexPatterns22() {
		KeyPattern left = maskPattern(String.class,
			mandatory(CONTENT_TYPE, 
				pattern(String.class, 
					mandatory(CONTENT_TYPE, "Rot13"))));
		KeyPattern right = pattern(String.class,
			optional(CONTENT_TYPE,  String.class),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(DISJOINT, c);
    }
	
	@Test
    public void complexPatterns23() {
		KeyPattern left = maskPattern(String.class,
			optional(CONTENT_TYPE, ANY_KEY));
		KeyPattern right = pattern(String.class,
			optional(CONTENT_TYPE, 
				pattern(String.class, 
					mandatory(CONTENT_TYPE, "Rot13"))),
			mandatory(RESOURCE_TYPE, String.class));
        int c = compare(left, right);
		
        assertEquals(RIGHT_RESTRICTION, c);
    }
}
